// pages/doctor-my-case-details/doctor-my-case-details.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    exampleId: '',
    content: "",
    replyId: "",
    pageNum: 1,
    pageSize: 5,
    caseinfo: [],
    casecommentinfo: [],
    caseid: '', // 案例id
    isreply: false, // 是否为回复 false.直接发布 true.回复
    replyContent: '', // 回复内容
    replyPeople: '', // 回复的人(拼接后的) 格式:回复 ***:
    replyid: '', // 评论指定id
    replyname: '', // 回复的人的昵称
    more: 0, // 上拉加载判断
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setStorageSync('entryType', true); // 从我的案例进入
    var that = this;
    var caseid = options.id;
    var isshowadd = options.isshowadd;
    that.setData({
      'caseid': caseid,
      'isshowadd': isshowadd
    })
    this.loadCaseDetail();
    this.getCaseComment();
    this.browse(caseid); //+浏览量
  },
  //获取案例评论
  getCaseComment: function () {
    var that = this;
    app.request({
      url: 'doctor/docExample/commentPage',
      data: {
        exampleId: this.data.caseid,
        pageSize: this.data.pageSize,
        pageNum: this.data.pageNum
      },
      success(res) {
        if (that.data.more == 0) {
          that.setData({
            'casecommentinfo': res.data,
          })
        } else {
          wx.hideLoading();
          that.setData({
            more: 0,
          });
          if (res.data.data.length) {
            var info = that.data.casecommentinfo.concat(res.data);
            that.setData({
              'casecommentinfo': info,
            })
          } else {
            wx.showToast({
              title: '已无更多...',
              icon: 'none',
              duration: 2000,
            });
          }
        }
      }
    })
  },
  // 案例详情
  loadCaseDetail: function() {
    var that = this;
    app.request({
      url: 'doctor/docExample/detail',
      data: {
        exampleId: this.data.caseid,
      },
      success(res) {
        that.setData({
          'caseinfo': res.data[0],
          'exampleId': res.data[0].exampleId,
        })
      }
    })
  },

  //浏览量增加
  browse: function() {
    var that = this;
    app.request({
      url: 'doctor/docExample/updateView',
      data: {
        exampleId: this.data.caseid,
      },
      success(res) {}
    })
  },

  // 处理点赞按钮
  handlePoint: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.exampleid;
    app.request({
      url: 'doctor/docExample/updatePoint',
      data: {
        exampleId: id,
      },
      success(res) {
        that.loadCaseDetail();
      }
    })
  },

  // 关注按钮
  bindFollow: function(e) {
    var isLogin = wx.getStorageSync('isLogin');
    var doctorid = e.currentTarget.dataset.doctorid;
    var that = this;
    app.request({
      url: 'doctor/member/attention',
      data: {
        userId: doctorid,
      },
      success(res) {
        that.loadCaseDetail();
      }
    })
  },

  // 回复指定人
  replyPeople: function(e) {
    
    var commentid = e.currentTarget.dataset.commentid; // 回复的案例id
    var nickname = e.currentTarget.dataset.nickname; // 回复人的昵称
    var replyPeople = '回复 ' + nickname + ':';
    this.setData({
      'replyid': commentid,
      'replyname': nickname,
      'replyPeople': replyPeople,
    })
  },

  // 回复内容
  replyContent: function(e) {
    this.setData({
      'replyContent': e.detail.value,
    })
  },

  // 处理回复
  bindReply: function(e) {
    var exampleId = this.data.caseid; // 案例id 
    var replyContent = this.data.replyContent; // 回复内容
    var replyPeople = this.data.replyPeople; // 回复的人 拼接后的
    if (!replyContent) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
      })
      return false;
    }
 
    var isReply = replyContent.indexOf(replyPeople); // 是否为回复人或者为直接发布
  
    var replyId = ''; // 评论指定id
    if (isReply == -1) {
      console.log('普通回复');
    } else {
      console.log('回复指定id', this.data.replyid);
      replyId = this.data.replyid;
      replyContent = replyContent.replace(replyPeople, '');
    }
    var that = this;
    app.request({
      url: 'doctor/docExample/comment',
      data: {
        exampleId: exampleId,
        content: replyContent,
        replyId: replyId,
      },
      success(res) {
       
        that.setData({
          'pageNum': 1,
        })
        that.getCaseComment();
        that.loadCaseDetail();
        that.setData({
          'replyPeople': '',
          'replyid': '',
          'replyContent': '',
        })
      }
    })
  },

  // 使页面滚动到底部
  pageScrollToBottom: function() {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function(rect) {
     
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.height
      })
    }).exec();
  },
  // 预览大图
  previewImage(e){
    const that = this;
    const urls = [];
    const imgUrl = this.data.imgUrl;
    const current = e.currentTarget.dataset.src;
    const imgArr = this.data.caseinfo.exampleImgs;
    for (let i = 0; i < imgArr.length; i++){
      urls.push(imgUrl+imgArr[i].imgPath)
    }
    wx.previewImage({
      urls
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      pageNum: this.data.pageNum + 1,
      'more': 1,
    })
   
    this.getCaseComment();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }
})